package com.chinasofti.manager.controller;

import com.chinasofti.manager.domain.Student;
import com.chinasofti.manager.service.StudentService;

import java.util.Scanner;

public class StudentController {
    StudentService studentService = new StudentService();
    Scanner sc = new Scanner(System.in);

    //开启学生管理系统
    public void start() {
        studentLoop:
        while (true) {
            System.out.println("------------------欢迎来到学生管理系统----------------------");
            System.out.println("1. 添加学生2. 删除学生3. 修改学生4. 查看学生5. 退出");
            String choice = sc.next();
            switch (choice) {
                case "1":
                    //System.out.println("添加学生");
                    addStudent();
                    break;
                case "2":
                    //System.out.println("删除学生");
                    deleteStudentById();
                    break;
                case "3":
                    //System.out.println("修改学生");
                    updateStudent();
                    break;
                case "4":
                    //System.out.println("查看学生");
                    findAllStudent();
                    break;
                case "5":
                    System.out.println("感谢使用学生管理系统");
                    break studentLoop;
                default:
                    System.out.println("您的输入有误");
                    break;
            }
        }
    }

    public void updateStudent() {
        System.out.println("输入合法的id");
        String updateId =inputStudentId();
        //手动录入更新对象
        Student newStu =inputStudentInfo(updateId);
        //进行修改
        studentService.updateStudent(updateId,newStu);
    }

    public void deleteStudentById() {
        String delId = inputStudentId();
      studentService.deleteStudentById(delId);
        System.out.println("删除成功");
    }

    private String inputStudentId() {
        String id ;
        while (true){
            System.out.println("请输入学生id");
            id=sc.next();
            boolean existi= studentService.isExists(id);
            if (existi){
                System.out.println("您输入的id不存在，请重新输入");
            }else {
                break;
            }
        }
        return  id;
    }

    public void findAllStudent() {
        Student[] stus = studentService.findAllStudent();
        //判断是否为空
       if(stus == null){
           System.out.println("查无信息，请添加后重试");
           return;
       }
       for (int i = 0; i <stus.length ; i++) {
           Student stu =stus[i];
           if (stu!=null){
               System.out.println("信息如下:" +  "学号:"+stu.getId()+"姓名："+stu.getName()+"年龄："+stu.getAge()+"生日："+stu.getBirthday());           }
       }
    }
    public void addStudent() {
        String id;
        while (true) {
            System.out.println("请输入学生的id");
            id = sc.next();
            boolean flag = studentService.isExists(id);
            if (flag) {
                System.out.println("学号可以使用");
                break;
            } else {
                System.out.println("学号被占用，请重新输入");
            }
        }
        //esSystem.out.println("准备添加学生");
        //封装学生对象
        Student stu = inputStudentInfo(id);
        //开始添加学生到数组
        boolean result = studentService.addStudent(stu);
        if (result) {
            System.out.println("添加成功");
        } else {
            System.out.println("添加失败");
        }
    }
    private Student inputStudentInfo(String id) {
        System.out.println("请输入学生姓名");
        String name = sc.next();
        System.out.println("请输入学生年龄");
        String age = sc.next();
        System.out.println("请输入学生生日");
        String birthday = sc.next();
        Student stu = new Student();
        stu.setId(id);
        stu.setAge(age);
        stu.setName(name);
        stu.setBirthday(birthday);
        return stu;
    }
}


